<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <a-row :gutter="24">
        <a-col :span="24 - gutter" v-if="deptFlag">
          <!-- 部门树 -->
            <dept-tree
              ref="deptTree"
              :deptOptions="deptOptions"
              @select="clickDeptNode"
            />
        </a-col>
        <a-col :span="gutter">
          <!-- 条件搜索 -->
          <div class="table-page-search-wrapper">
            <a-form layout="inline">
              <a-row :gutter="48">
                <a-col :md="8" :sm="24">
                  <a-form-item label="学员名称" prop="studentName">
                    <a-input v-model="queryParam.studentName" placeholder="请输入学员名称" allow-clear/>
                  </a-form-item>
                </a-col>
                <a-col :md="8" :sm="24">
                  <a-form-item label="商机归属人" prop="managerName">
                    <a-input v-model="queryParam.managerName" placeholder="请输入商机归属人" allow-clear/>
                  </a-form-item>
                </a-col>
                <template v-if="advanced">
                  <a-col :md="8" :sm="24">
                    <a-form-item label="班型" prop="classType">
                      <a-select placeholder="请选择班型" v-model="queryParam.classType" style="width: 100%" allow-clear>
                        <a-select-option v-for="(d, index) in classTypeOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                      </a-select>
                    </a-form-item>
                  </a-col>
                  <a-col :md="8" :sm="24">
                    <a-form-item label="按月份查询" prop="month">
                      <a-month-picker placeholder="请选择月份" @change="onChange" >
                        <a-icon slot="suffixIcon" type="smile" />
                      </a-month-picker>
                    </a-form-item>
                  </a-col>
                  <a-col :md="8" :sm="24">
                    <a-form-item label="到期次数" prop="count">
                      <a-input v-model="queryParam.count" placeholder="请输入到期次数" allow-clear/>
                    </a-form-item>
                  </a-col>
                </template>
                <a-col :md="!advanced && 8 || 24" :sm="24">
                  <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                    <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                    <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
                    <a @click="toggleAdvanced" style="margin-left: 8px">
                      {{ advanced ? '收起' : '展开' }}
                      <a-icon :type="advanced ? 'up' : 'down'"/>
                    </a>
                  </span>
                </a-col>
              </a-row>
            </a-form>
          </div>
          <!-- 操作 -->
          <div class="table-operations">
            <!-- <a-button type="primary" @click="$refs.createForm.handleAdd(queryParam.deptId)" v-hasPermi="['manager:student:add']">
              <a-icon type="plus" />新增
            </a-button> -->
            <a-button type="primary" :disabled="multiple" @click="openDelay = true">
              <a-icon type="edit" />延期
            </a-button>
            <!-- <a-button type="primary" @click="handleExport" v-hasPermi="['manager:student:export']">
              <a-icon type="download" />导出
            </a-button> -->
            <table-setting
              :style="{float: 'right'}"
              :table-size.sync="tableSize"
              v-model="columns"
              :refresh-loading="loading"
              @refresh="getList" />
          </div>
          <!-- 增加修改 -->
          <create-form
            ref="createForm"
            :sourceOptions="sourceOptions"
            :intentionOptions="intentionOptions"
            :sexOptions="sexOptions"
            :statusOptions="statusOptions"
            :treeData="deptOptions"
            :classTypeOptions="classTypeOptions"
            @ok="getList"
          />
          <!-- 数据展示 -->
          <a-table
            :loading="loading"
            :size="tableSize"
            rowKey="id"
            :columns="columns"
            :data-source="list"
            :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
            :pagination="false"
            :bordered="tableBordered"
          >
            <span slot="operation" slot-scope="text, record">
              <a @click="changeDelayForm.id = record.id;openChangeDelayForm=true;">
                <a-icon type="edit" />修改
              </a>
            </span>
            <span slot="classType" slot-scope="text, record">
              {{ classTypeFormat(record) }}
            </span>
          </a-table>
          <!-- 分页 -->
          <a-pagination
            class="ant-table-pagination"
            show-size-changer
            show-quick-jumper
            :current="queryParam.pageNum"
            :total="total"
            :page-size="queryParam.pageSize"
            :showTotal="total => `共 ${total} 条`"
            @showSizeChange="onShowSizeChange"
            @change="changeSize"
          />
        </a-col>
      </a-row>
    </a-card>
    <!-- 延期处理 -->
    <a-modal v-model="openDelay" title="延期处理" @ok="handleDelay">
      <a-form >
          <a-form-model-item label="天数" prop="days" >
            <a-input v-model="delayForm.days" placeholder="请输入延期天数" allow-clear/>
          </a-form-model-item>
          <a-form-model-item label="备注" prop="remark" >
            <a-input v-model="delayForm.remark" placeholder="请输入备注" allow-clear/>
          </a-form-model-item>
      </a-form>
    </a-modal>
    
    <!-- 修改剩余课次 -->
    <a-modal v-model="openChangeDelayForm" title="修改剩余课次" @ok="handlechangeRemainCount">
      <a-form >
          <a-form-model-item label="课次" prop="count" >
            <a-input v-model="changeDelayForm.count" placeholder="请输入课次" allow-clear/>
          </a-form-model-item>
          <a-form-model-item label="备注" prop="remark" >
            <a-input v-model="changeDelayForm.remark" placeholder="请输入备注" allow-clear/>
          </a-form-model-item>
      </a-form>
    </a-modal>

  </page-header-wrapper>
</template>

<script>
import { delayPage,delay,changeRemainCount} from '@/api/manager/studentCard'
import CreateForm from './modules/CreateForm'
import { tableMixin } from '@/store/table-mixin'
import storage from'store'
import DeptTree from '../../system/user/modules/DeptTree'
import { treeselect } from '@/api/system/dept'
import moment from 'moment'

export default {
  name: 'Delay',
  components: {
    CreateForm,
    DeptTree
  },
  mixins: [tableMixin],
  data () {
    return {
      changeDelayForm:{},
      openChangeDelayForm:false,
      openDelay:false,
      dataSource:[],
      value:null,
      delayForm:{
        days:null,
        ids:[]
      },
      openProxy:false,
      // 部门树选项
      deptOptions: [{
        id: 0,
        label: '',
        children: []
      }],
      gutter:24,
      deptFlag: false,
      list: [],
      selectedRowKeys: [],
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      ids: [],
      loading: false,
      total: 0,
      // 商机来源字典
      sourceOptions: [],
      classTypeOptions:[],
      //班型
      classTypeOptions:[],
      // 商机意向字典
      intentionOptions: [],
      // 性别字典
      sexOptions: [],
      // 状态字典
      statusOptions: [],
      // 查询参数
      queryParam: {
        deptId:null,
        pageNum: 1,
        pageSize: 10
      },
      columns: [
        {
          title: '学员名称',
          dataIndex: 'studentName',
          ellipsis: true,
          fixed: 'left',
          align: 'center'
        },
        {
          title: '服务销售',
          dataIndex: 'managerName',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '会员卡',
          dataIndex: 'cardName',
          ellipsis: true,
          align: 'center'
        },{
          title: '班型',
          dataIndex: 'classType',
          ellipsis: true,
          scopedSlots: { customRender: 'classType' },
          align: 'center'
        },
        {
          title: '剩余次数',
          dataIndex: 'count',
          ellipsis: true,
          align: 'center'
        },{
          title: '开卡时间',
          dataIndex: 'startTime',
          ellipsis: true,
          align: 'center'
        },{
          title: '到期时间',
          dataIndex: 'endTime',
          ellipsis: true,
          align: 'center'
        },{
          title: '操作',
          fixed: 'right',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          align: 'center'
        }
      ]
    }
  },
  filters: {
  },
  created () {
    this.deptInit();
    this.getTreeselect()
    this.getList()
    this.getDicts('class_type').then(response => {
      this.classTypeOptions = response.data
    })
  },
  computed: {
  },
  watch: {
  },
  methods: {
    classTypeFormat (row, column) {
      return this.selectDictLabel(this.classTypeOptions, row.classType)
    },
    handlechangeRemainCount(){
      changeRemainCount(this.changeDelayForm).then(resp => {
        if(resp.code == 200){
          this.$message.success('修改剩余课次成功');
          this.openChangeDelayForm = false;
          this.getList();
        }else{
          this.$message.error(resp.msg);
        }
      });
    },
    onChange(value){
      if(value){
        this.queryParam.month = moment(value).format("YYYY-MM");
      }else{
        this.queryParam.month = null;
      }
    },
    // 跳转
    toCardPage(record){
      if(!record.classType){
        this.$message.error('请先为学员选择班型！');
        return;        
      }
      this.$router.push({path:'/business/studentCard',query: {studentId:record.id,deptId:this.queryParam.deptId}})
    },
    toTrackPage(record){
      if(!record.classType){
        this.$message.error('请先为学员选择班型！');
        return;        
      }
      this.$router.push({path:'/schedule/list',query: {userId:record.id}})
    },
    changeValue(value,option){
      this.proxyForm.userId = option.data.key
    },
    handleDelay(){
      if(!this.delayForm.days){
        this.$message.error('请填写延期天数！');
        return;
      }
      this.delayForm.ids = this.ids;
      this.delayForm.deptId = this.queryParam.deptId;
      delay(this.delayForm).then(resp => {
        this.$message.success('延期成功');
        this.openDelay = false;
        this.getList();
      });
    },
    deptInit(){
      this.deptFlag = storage.get("deptId") == 100;
      this.queryParam.deptId = this.$route.query.deptId;
      if(this.queryParam.deptId == null){
        this.queryParam.deptId = storage.get("deptId")
      }
      
      if(this.deptFlag){
        this.gutter = 20;
      }else{
        this.gutter = 24;
      }
    },
    /** 查询部门下拉树结构 */
    getTreeselect () {
      treeselect().then(response => {
        this.deptOptions = response.data
      })
    },
    clickDeptNode (deptId) {
      this.queryParam.deptId = deptId
      this.handleQuery()
    },
    /** 查询学员信息列表 */
    getList () {
      this.loading = true
      delayPage(this.queryParam).then(response => {
        this.list = response.rows
        console.log(this.list);
        this.total = response.total
        this.loading = false
      })
    },
    // 商机来源字典翻译
    sourceFormat (row, column) {
      return this.selectDictLabel(this.sourceOptions, row.source)
    },
    // 商机意向字典翻译
    intentionFormat (row, column) {
      return this.selectDictLabel(this.intentionOptions, row.intention)
    },
    // 性别字典翻译
    sexFormat (row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex)
    },
    // 状态字典翻译
    statusFormat (row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParam = {
        name: undefined,
        avater: undefined,
        cardNo: undefined,
        birthday: undefined,
        curator: undefined,
        phone: undefined,
        source: undefined,
        ascription: undefined,
        ascriptionSale: undefined,
        intention: undefined,
        product: undefined,
        age: undefined,
        sex: undefined,
        height: undefined,
        weight: undefined,
        school: undefined,
        address: undefined,
        proxyId: undefined,
        nature: undefined,
        status: undefined,
        deptId: undefined,
        pageNum: 1,
        pageSize: 10
      }
      this.handleQuery()
    },
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    /** 导出按钮操作 */
    handleExport () {
      var that = this
      this.$confirm({
        title: '是否确认导出?',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk () {
          that.download('manager/student/export', {
            ...that.queryParam
          }, `student_${new Date().getTime()}.xlsx`)
        },
        onCancel () {}
      })
    }
  }
}
</script>
